
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>zipimport --- 从 Zip 存档中导入模块 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="pkgutil --- 包扩展模块工具" href="pkgutil.html" />
    <link rel="prev" title="导入模块" href="modules.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/zipimport.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="pkgutil.html" title="pkgutil --- 包扩展模块工具"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="modules.html" title="导入模块"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">导入模块</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-zipimport">
<span id="zipimport-import-modules-from-zip-archives"></span><h1><a class="reference internal" href="#module-zipimport" title="zipimport: Support for importing Python modules from ZIP archives."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code></a> --- 从 Zip 存档中导入模块<a class="headerlink" href="#module-zipimport" title="永久链接至标题">¶</a></h1>
<hr class="docutils" />
<p>此模块添加了从 ZIP 格式档案中导入 Python 模块（ <code class="file docutils literal notranslate"><span class="pre">*.py</span></code> ， <code class="file docutils literal notranslate"><span class="pre">*.pyc</span></code> ）和包的能力。通常不需要明确地使用 <a class="reference internal" href="#module-zipimport" title="zipimport: Support for importing Python modules from ZIP archives."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code></a> 模块，内置的 <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> 机制会自动将此模块用于 ZIP 档案路径的 <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 项目上。</p>
<p>通常， <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 是字符串的目录名称列表。此模块同样允许 <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的一项成为命名 ZIP 文件档案的字符串。 ZIP 档案可以容纳子目录结构去支持包的导入，并且可以将归档文件中的路径指定为仅从子目录导入。比如说，路径 <code class="file docutils literal notranslate"><span class="pre">example.zip/lib/</span></code> 将只会从档案中的 <code class="file docutils literal notranslate"><span class="pre">lib/</span></code> 子目录导入。</p>
<p>任何文件都可以存在于 ZIP档案之中，但是只有 <code class="file docutils literal notranslate"><span class="pre">.py</span></code> 和 <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> 文件是能够导入的。不允许导入 ZIP 中的动态模组（ <code class="file docutils literal notranslate"><span class="pre">.pyd</span></code> ， <code class="file docutils literal notranslate"><span class="pre">.so</span></code> ）。请注意，如果档案中只包含 <code class="file docutils literal notranslate"><span class="pre">.py</span></code> 文件， Python不会尝试通过添加对应的 <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> 文件修改档案，意思是如果 ZIP 档案不包含 <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> 文件，导入或许会变慢。</p>
<p>目前不支持带有档案注释的 ZIP 归档。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP Application Note</a></dt><dd><p>Phil Katz 编写的 ZIP 文件格式文档，此格式和使用的算法的创建者。</p>
</dd>
<dt><span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0273"><strong>PEP 273</strong></a> -  从ZIP压缩包导入模块</dt><dd><p>由 James C. Ahlstrom 编写，他还提供了一个具体实现。 Python 2.3 遵循 PEP 273 的规范，但使用了一个由 van Rossum 本人所编写的实现，该实现使用了 PEP 302 所描述的导入钩子。</p>
</dd>
<dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> - 新导入钩</dt><dd><p>PEP 添加导入钩来有助于模块运作。</p>
</dd>
</dl>
</div>
<p>此模块定义了一个异常：</p>
<dl class="exception">
<dt id="zipimport.ZipImportError">
<em class="property">exception </em><code class="sig-prename descclassname">zipimport.</code><code class="sig-name descname">ZipImportError</code><a class="headerlink" href="#zipimport.ZipImportError" title="永久链接至目标">¶</a></dt>
<dd><p>异常由 zipimporter 对象引发。这是 <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> 的子类，因此，也可以捕获为 <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> 。</p>
</dd></dl>

<div class="section" id="zipimporter-objects">
<span id="id1"></span><h2>zipimporter 对象<a class="headerlink" href="#zipimporter-objects" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#zipimport.zipimporter" title="zipimport.zipimporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">zipimporter</span></code></a> 是用于导入 ZIP 文件的类。</p>
<dl class="class">
<dt id="zipimport.zipimporter">
<em class="property">class </em><code class="sig-prename descclassname">zipimport.</code><code class="sig-name descname">zipimporter</code><span class="sig-paren">(</span><em class="sig-param">archivepath</em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter" title="永久链接至目标">¶</a></dt>
<dd><p>创建新的 zipimporter 实例。 <em>archivepath</em> 必须是指向 ZIP 文件的路径，或者 ZIP 文件中的特定路径。例如， <code class="file docutils literal notranslate"><span class="pre">foo/bar.zip/lib</span></code> 的 <em>archivepath</em> 将在 ZIP 文件 <code class="file docutils literal notranslate"><span class="pre">foo/bar.zip</span></code> 中的 <code class="file docutils literal notranslate"><span class="pre">lib</span></code> 目录中查找模块。</p>
<p>如果 <em>archivepath</em> 没有指向一个有效的 ZIP 档案，引发 <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> 。</p>
<dl class="method">
<dt id="zipimport.zipimporter.find_module">
<code class="sig-name descname">find_module</code><span class="sig-paren">(</span><em class="sig-param">fullname</em><span class="optional">[</span>, <em class="sig-param">path</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.find_module" title="永久链接至目标">¶</a></dt>
<dd><p>搜索由 <em>fullname</em> 指定的模块。 <em>fullname</em> 必须是完全合格的（含加点作分割的拓展名）模块名。它返回 zipimporter 实例本身如果模块被找到，或者返回 <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> 如果没找到指定模块。可选的 <em>path</em> 被忽略，这是为了与导入器协议兼容。</p>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_code">
<code class="sig-name descname">get_code</code><span class="sig-paren">(</span><em class="sig-param">fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.get_code" title="永久链接至目标">¶</a></dt>
<dd><p>返回指定模块的代码对象。如果不能找到模块会引发 <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> 错误。</p>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_data">
<code class="sig-name descname">get_data</code><span class="sig-paren">(</span><em class="sig-param">pathname</em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.get_data" title="永久链接至目标">¶</a></dt>
<dd><p>返回与 <em>pathname</em> 相关联的数据。如果不能找到文件则引发 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 错误。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span><a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a> 代替 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 被引发。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_filename">
<code class="sig-name descname">get_filename</code><span class="sig-paren">(</span><em class="sig-param">fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.get_filename" title="永久链接至目标">¶</a></dt>
<dd><p>如果导入了指定的模块 <code class="docutils literal notranslate"><span class="pre">__file__</span></code> ，则返回为该模块设置的值。如果未找到模块则引发 <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> 错误。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.1 新版功能.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_source">
<code class="sig-name descname">get_source</code><span class="sig-paren">(</span><em class="sig-param">fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.get_source" title="永久链接至目标">¶</a></dt>
<dd><p>返回指定模块的源代码。如果没有找到模块则引发 <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> ，如果档案包含模块但是没有源代码，返回 <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> 。</p>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.is_package">
<code class="sig-name descname">is_package</code><span class="sig-paren">(</span><em class="sig-param">fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.is_package" title="永久链接至目标">¶</a></dt>
<dd><p>如果由 <em>fullname</em> 指定的模块是一个包则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> 。如果不能找到模块则引发 <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> 错误。</p>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.load_module">
<code class="sig-name descname">load_module</code><span class="sig-paren">(</span><em class="sig-param">fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.load_module" title="永久链接至目标">¶</a></dt>
<dd><p>加载由 <em>fullname</em> 指定的模块。 <em>fullname</em> 必须是完全合格的（含加点作为分割的拓展名）模块名。它返回已加载模块，或者当找不到模块时引发 <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> 错误。</p>
</dd></dl>

<dl class="attribute">
<dt id="zipimport.zipimporter.archive">
<code class="sig-name descname">archive</code><a class="headerlink" href="#zipimport.zipimporter.archive" title="永久链接至目标">¶</a></dt>
<dd><p>导入器关联的 ZIP 文件的文件名，不含可能的子路径。</p>
</dd></dl>

<dl class="attribute">
<dt id="zipimport.zipimporter.prefix">
<code class="sig-name descname">prefix</code><a class="headerlink" href="#zipimport.zipimporter.prefix" title="永久链接至目标">¶</a></dt>
<dd><p>ZIP 文件中搜索的模块的子路径。这是一个指向 ZIP 文件 根目录的 zipimporter 对象的空字符串。</p>
</dd></dl>

<p>当与斜杠结合使用时， <a class="reference internal" href="#zipimport.zipimporter.archive" title="zipimport.zipimporter.archive"><code class="xref py py-attr docutils literal notranslate"><span class="pre">archive</span></code></a> 和 <a class="reference internal" href="#zipimport.zipimporter.prefix" title="zipimport.zipimporter.prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">prefix</span></code></a> 属性等价于赋予 <a class="reference internal" href="#zipimport.zipimporter" title="zipimport.zipimporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">zipimporter</span></code></a> 构造器的原始 <em>archivepath</em> 参数。</p>
</dd></dl>

</div>
<div class="section" id="examples">
<span id="zipimport-examples"></span><h2>例子<a class="headerlink" href="#examples" title="永久链接至标题">¶</a></h2>
<p>这是一个从 ZIP 档案中导入模块的例子 - 请注意 <a class="reference internal" href="#module-zipimport" title="zipimport: Support for importing Python modules from ZIP archives."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code></a> 不需要明确地使用。</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> unzip -l example.zip
<span class="go">Archive:  example.zip</span>
<span class="go">  Length     Date   Time    Name</span>
<span class="go"> --------    ----   ----    ----</span>
<span class="go">     8467  11-26-02 22:30   jwzthreading.py</span>
<span class="go"> --------                   -------</span>
<span class="go">     8467                   1 file</span>
<span class="gp">$</span> ./python
<span class="go">Python 2.3 (#1, Aug 1 2003, 19:54:32)</span>
<span class="gp">&gt;</span>&gt;&gt; import sys
<span class="gp">&gt;</span>&gt;&gt; sys.path.insert<span class="o">(</span><span class="m">0</span>, <span class="s1">&#39;example.zip&#39;</span><span class="o">)</span>  <span class="c1"># Add .zip file to front of path</span>
<span class="gp">&gt;</span>&gt;&gt; import jwzthreading
<span class="gp">&gt;</span>&gt;&gt; jwzthreading.__file__
<span class="go">&#39;example.zip/jwzthreading.py&#39;</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code> --- 从 Zip 存档中导入模块</a><ul>
<li><a class="reference internal" href="#zipimporter-objects">zipimporter 对象</a></li>
<li><a class="reference internal" href="#examples">例子</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="modules.html"
                        title="上一章">导入模块</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="pkgutil.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pkgutil</span></code> --- 包扩展模块工具</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/zipimport.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="pkgutil.html" title="pkgutil --- 包扩展模块工具"
             >下一页</a> |</li>
        <li class="right" >
          <a href="modules.html" title="导入模块"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="modules.html" >导入模块</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>